﻿using System;
using RAP.Framework.Libary.DomTemplateBuilder.Element.StringActions;
using RAP.Framework.Libary.DomTemplateBuilder.Interface;

namespace RAP.Framework.Libary.DomTemplateBuilder.Handler.StringActions
{
	/// <summary>
	/// 
	/// </summary>
	public class StringIsEndWithHandler : StringActionConditionalElementHandler
    {
        public new StringIsEndWith Element
        {
            get
            {
				return base.Element as StringIsEndWith;
            }
        }

        public override bool IsCondition ( object rootParamObject, object parentParamObject, ITemplateRenderContext context )
        {
			var exp = this.GetPropertyValue ( rootParamObject, parentParamObject, context ) as string;
            if ( string.IsNullOrEmpty ( exp ) )
            {
                return false;
            }
			var find = ExpressionUtils.GetFieldValue
            (
                this.Element.Find
                , rootParamObject
                , parentParamObject
                , context
                , ( ) => { return this.Element.Find; }
            ) as string;

			var comparisonType = this.Element.ComparisonType;

			var finds = find.Split ( Consts.STRING_SPLIT_CHAR, StringSplitOptions.RemoveEmptyEntries );
			foreach ( var val in finds )
			{
				if ( exp.EndsWith ( val.Trim ( ), comparisonType ) )
				{
					return true;
				}
			}
			return false;
        }


    }
}
